// 封装banner轮播图代码
import { onMounted, ref } from 'vue'
import { getCategoryAPI } from '@/apis/categroy'
import { useRoute } from 'vue-router'
import { onBeforeRouteUpdate } from 'vue-router/dist/vue-router'

export function useCategory() {
  const categoryData = ref({})
  const route = useRoute()

  const getCategory = async (id = route.params.id) => {
    const { result } = await getCategoryAPI(id)
    categoryData.value = result
  }

  onMounted(() => getCategory())

  // 在路由参数变化的时候 可以把分类数据接口重新发送
  onBeforeRouteUpdate((to) => {
    // 使用最新的路由参数请求最新的分类数据
    const { id } = to.params
    getCategory(id)
  })

  return {
    categoryData
  }
}
